/**
* 
* ====================================================================
*
* @author apov <apov@ymail.com>
*/ 
#pragma once

#include <BaseDefine.h>
#include <Socket.h>
#include <LumpData.h>
#include <Work.h>
#include <Thread.h>
#include "NetParser.h"

/**
* @class NetworkSession
*  - 
*/
class NetworkSession : public Work{
	enum{
		DEF_DEFAULT_TYPE=			0,	
		DEF_TIME_OUT_ADD_VALUE=		5,		
	};

public:	
	virtual bool initialize( Socket socket, bool isBlock = false ) = 0;
	virtual int read_handler( int recv_size = 0 ) = 0;
	virtual int write_handler( int write_size = 0 ) = 0;
	virtual void close_handler( int type, const char* caller_name = 0 ) = 0;
	
	virtual int write(char* data, size_t size) = 0;
	virtual int write( char* data, size_t size, bool immediate ) = 0;
	virtual int write( bool lock = true ) = 0;	
	virtual void set_parser_type( int parser_type );

	bool isClose();	
	size_t isCloseReserve(){ return close_reserve_; }
	void closeReserve( char* msg = 0 );
	bool isInputTimeout( size_t limit_time, size_t last_sec );
	bool isDeadTimeout( size_t limit_time, size_t last_sec );
	void set_extern_instance( void* data );
	void* get_extern_instance();

	SOCKET getPeer();
	SOCKET getInvalidPeer();
	Socket* getPeerInstance();

	size_t getActivateTime();

	void setIdent( unsigned int ident ){
		ident_ = ident;
	}

	unsigned int getIdent(){
		return ident_;
	}

	virtual LumpData* getRecvBuffer() = 0;

	NetworkSession();
	virtual ~NetworkSession();

protected:
	void reset();

	unsigned int ident_;
	Socket peer_;
	
	bool closed_;
	size_t close_reserve_;

	size_t last_input_sec_;
	size_t start_activate_time_;
	
	NetParser* parser_;

private:
	void* extern_instance_;
};
